// pages/optional/major/index.js
const {
  twoMajorList,
} = require('../../../api/api')
const selectorRouteDatas = [{
    id: 0,
    title: "全国",
    type: "radiobox",
    key: "province",
    items: []
  },
  {
    id: 1,
    title: "年份",
    type: "radiobox",
    key: "year",
    items: [
      {
        id: '2024',
        title: '2024'
      },
    ]
  },


]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filterRouteKey: 'optionalChooseFilters',
    selectedData: {
      province: 0,
      year: '2024'
    },
    selectorDataList: selectorRouteDatas,
    setting: {
      width: '220rpx',
      weight: 'normal',
      color: '#808080',
      fontSize: '26rpx',
      height: '50rpx',
      padding: '0'
    },
    categoryList: [],
    currentIndex: 0,
    selectedList: [],
    selectedIds: [],
    mainHeight: getApp().globalData.mainHeight,
    visible: false,
    selectProps: {
      name: 'title',
      id: 'id'
    },
    level2Name: '',
    planMatchId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options) {
    if (options.params) {
      const params = JSON.parse(decodeURIComponent(options.params))
      this.setData({
        ...params
      })
    }
    const {
      filterRouteKey,
      selectorDataList,
    } = this.data
    var d=new Date()
    var year=d.getFullYear()
    var month=d.getMonth()
    if(month>6){
      year=year+1
    }
    var len=year+1
    var yearList=[]
    for(var i=year;i<len;i++){
      yearList.push({id:i,title:i})
    }
    selectorDataList[1].items=yearList
    this.setData({
      selectorDataList
    })
    if (!wx.getStorageSync(filterRouteKey)) {
      let obj = {
        province: wx.getStorageSync('optionalFilters').province,
        year: year
      }
      this.setData({
        selectedData: obj
      })
      wx.setStorageSync(filterRouteKey, obj)
    } else {
      this.setData({
        selectedData: wx.getStorageSync(filterRouteKey)
      })
    }
    this.fetchCity()
    this.fetchCategory()
  },
  async fetchCity() {
    const {
      type,
      selectorDataList
    } = this.data
    const cityList = wx.getStorageSync('cityList') || await getApp().getCityList()
    selectorDataList[0].items = cityList
    this.setData({
      selectorDataList
    })
    this.filterKeyFunc()
  },
  async fetchCategory() {
    this.setData({
      currentIndex: 0
    })
    const params = {
      level2Name: this.data.level2Name
    }
    const {
      result
    } = await twoMajorList(params)
    const categoryList = result.map((item) => {
      const children = item.children.map((child) => {
        return {
          title: child.name,
          id: child.id
        }
      })
      return {
        id: item.id,
        title: item.name,
        children
      };
    })
    this.setData({
      categoryList,
    })
    this.filterMajor()
  },
  filterKeyFunc() {
    const selectedData = wx.getStorageSync(this.data.filterRouteKey)
    const {
      selectorDataList
    } = this.data
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : item.title
    })
    this.setData({
      selectorDataList,
      selectedData
    })

  },
  async onSelectChangedRoute(e) {
    wx.setStorageSync(this.data.filterRouteKey, {
      ...e.detail
    });
    this.filterKeyFunc()
    this.setData({
      ...e.detail
    });
  },
  inputChange(e) {
    this.setData({
      level2Name: e.detail.value
    })
  },

  //意向专业改变
  onSelectChange(e) {
    const {
      selectedList,
      selectedIds
    } = e.detail
    const {
      categoryList
    } = this.data
    if (e.detail.id) {
      categoryList.map((item) => {
        item.children.map((child) => {
          child.checked = selectedIds.indexOf(child.id) > -1;
        })
      })
    } else {
      categoryList.map((item) => {
        item.children.map((child) => {
          child.checked = false
        })
      })
    }
    this.setData({
      categoryList,
      selectedList,
      selectedIds
    })
  },
  tabSelected(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.setData({
      currentIndex: index
    })
  },
  toggleChecked(evt) {
    const {
      checked,
      id,
      value
    } = evt.currentTarget.dataset;
    let selectedIds = this.data.selectedIds
    if (checked) {
      selectedIds.splice(selectedIds.indexOf(id), 1)
    } else {
      if (selectedIds.length >= 20) return
      selectedIds.push(id);
    }
    this.setData({
      selectedIds
    })
    this.filterMajor()
  },
  filterMajor() {
    const {
      categoryList,
      selectedIds
    } = this.data
    const selectedList = []
    categoryList.map((item) => {
      item.children.map((child) => {
        child.checked = selectedIds.indexOf(child.id) > -1;
        if (selectedIds.indexOf(child.id) > -1) {
          selectedList.push(child)
        }
      })
    })
    this.setData({
      categoryList,
      selectedList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '专业选科',
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})